function get_reader_info() {
    if ($("#reader_barcode").val() == "") {
        alert("没有输入条形码")
        return
    }
    var data = {reader: $("#reader_barcode").val()}
    console.log(data)
    var url = "/admin/get_reader_info"
    var success = function (data) {
        if (data.reader_status == false){
          return  alert("条形码错误")
        }
        if (data.borr_status == false){
            write_user_info(data.reader_info)
            return  alert("该读者没有借阅历史")
        }
        write_user_info(data.reader_info)
        write_return_html(data.borrow_book_info)
    }
    var error = function () {
        alert("error")
    }
    post_ajax(url, data, success, error)
}
function get_reader_borrow_info() {
    if ($("#reader_barcode").val() == "") {
        alert("没有输入条形码")
        return
    }
    var data = {reader_code: $("#reader_barcode").val()}
    console.log(data)
    var url = "/admin/get_reader_borrow_info"
    var success = function (data) {
        console.log(data)
        write_user_info(data)
    }
    var error = function () {

    }
    post_ajax(url, data, success, error)

}

function write_user_info(data) {
    $("#name").val(data.name);
    $("#sex").val(data.sex);
    $("#user_type").val(data.user_type);
    $("#Type").val(data.Type);
    $("#peperNO").val(data.peperNO);
}

function borrow_book() {
    if ($("#book_code").val() == "") {
        alert("没有输入图书条形码")
        return
    }
    var data = {
        book_barcode: $("#book_code").val(),
        user_barcode: $("#reader_barcode").val()
    }
    console.log(data)
    var url = "/admin/borrow_book"
    var success = function (data) {
        write_html(data)
        alert("借阅完成")
    }
    var error = function () {

    }
    post_ajax(url, data, success, error)
}

function write_html(data) {
    var html = "<div class='book-info-div'>" +
        "<div class='book-info-center-tool tool-right-border text-center'>" + data.book_name + "</div>" +
        "<div class='book-info-center-tool tool-right-border text-center'>" + data.borrow_time + "</div>" +
        "<div class='book-info-center-tool tool-right-border text-center'>" + data.shouldbacktime + "</div>" +
        "<div class='book-info-center-tool tool-right-border text-center'>" + data.publish + "</div>" +
        "<div class='book-info-center-tool tool-right-border text-center'>" + data.book_type + "</div>" +
        "<div class='book-info-center-tool tool-right-border text-center'>" + data.book_money + "</div>"

    $("#book_info_list").append(html)
}

function write_return_html(data) {
    for (var i=0;i<data.length;i++)
    {
        console.log(data[i])
        var html = "<div class='book-info-div' id="+data[i].borrow_id+">" +
            "<div class='book-info-center-tool tool-right-border text-center'>" + data[i].book_name + "</div>" +
            "<div class='book-info-center-tool tool-right-border text-center'>" + data[i].borrow_time + "</div>" +
            "<div class='book-info-center-tool tool-right-border text-center'>" + data[i].shouldbacktime + "</div>" +
            "<div class='book-info-center-tool tool-right-border text-center'>" + data[i].publish + "</div>" +
            "<div class='book-info-center-tool tool-right-border text-center'>" + data[i].book_type + "</div>" +
            "<div class='book-info-center-tool tool-right-border text-center'>" + "<a onclick='return_book(" + data[i].borrow_id + ")'>" + "归还" + "</a>" + "</div>"

        $("#book_info_list").append(html)
    }

}

function return_book(borrow_id) {
    console.log(borrow_id)
    var data = {borrow_id: borrow_id}
    console.log(data)
    var url = "/admin/return_book"
    var success = function (data) {
        alert("归还成功")
        $("#"+data.borrow_id).addClass("hidden")

    }
    var error = function () {

    }
    post_ajax(url, data, success, error)

}